﻿#include "SMTExtFuncDemo.h"
#include <QDebug>
#include <QTime>
#include <QUuid>
#include <QDir>

#define HanleParamOutQuick(i, val)		if (m_paraOutput[i].enabled())\
{\
	m_paraOutput[i].setValid(true);\
	m_paraOutput[i].setValue(val);\
}\
else\
{\
	m_paraOutput[i].setValid(false);\
}

struct SmtExtInfo
{
	SMTExtFuncPageUI *m_page;
};

SMTExtFuncDemo::SMTExtFuncDemo()
{
	m_algorithm = new SmtExtInfo();
	((SmtExtInfo*)m_algorithm)->m_page = nullptr;

	initOutPutVariables();
	initInputVariables();
	initProperties();
	initSettingPages();
}

SMTExtFuncDemo::~SMTExtFuncDemo()
{
	SmtExtInfo *algorithm = (SmtExtInfo*)m_algorithm;
	delete algorithm->m_page;
	algorithm->m_page = nullptr;
	delete m_algorithm;
	m_algorithm = nullptr;
}

//杩愯鍑芥暟
long SMTExtFuncDemo::run()
{
	SMTLogGenerator logGenerator;
	SMTString uniqueID;
	bool bStatus = false;
	if (this->parent())
	{
		uniqueID = SMTString("%1.%2").arg(this->parent()->name()).arg(this->name());
	}
	SMTString debugInfo(QObject::tr("SMTExtFuncDemo run start.").toUtf8().constData());
	logGenerator.writeInfo(SMTLogGenerator::LV_DEBUG, this->toolIndex(), uniqueID, debugInfo);

	SMTPoint inputPoint;
	SMTNumber OffsetX;
	SMTNumber OffsetY;


	bStatus = true;
	//鑾峰彇input閾捐〃涓殑鍊?
	SMTVariant val;
	m_paraInput[0].getValue(&val);
	val.toPoint(&inputPoint);

	m_paraInput[1].getValue(&val);
	val.toNumber(&OffsetX);

	m_paraInput[2].getValue(&val);
	val.toNumber(&OffsetY);

	//杩愯閮ㄥ垎
	SMTPoint OutputPoint;
	double x = inputPoint.x() + OffsetX.toDouble();
	double y = inputPoint.y() + OffsetY.toDouble();
	OutputPoint.setX(inputPoint.x() + OffsetX.toDouble());
	OutputPoint.setY(inputPoint.y() + OffsetY.toDouble());

	SMTShape displayPoint;
	displayPoint.fromPoint(OutputPoint);

	SMTShape OriginalPoint;
	OriginalPoint.fromPoint(inputPoint);

	DisplayROI(OriginalPoint, 0);

	DisplayROI(displayPoint, 1);

	HanleParamOutQuick(0, bStatus);

	HanleParamOutQuick(1, OriginalPoint);

	HanleParamOutQuick(2, OutputPoint);
	return 0;
}

//鏄剧ずROI鍑芥暟
void SMTExtFuncDemo::DisplayROI(SMTShape &shape, int index)
{
	SMTGraphicElement elem(SMTShape::SMT_SHAPE_POINT);
	m_graphics.getElement(index, &elem);
	SMTShapeArray shapeArray;
	shapeArray.push_back(shape);
	elem.setShapeArray(shapeArray);
	m_graphics.replaceElement(index, elem);
}

//璁剧疆椤甸潰鏁伴噺
int SMTExtFuncDemo::SettingPageCount()
{
	return 1;
}

//鍒涘缓椤甸潰
SMTSettingPage* SMTExtFuncDemo::createSettingPage(int index)
{
	if (index == 0)
	{
		//鐢熸垚骞跺垵濮嬪寲鍙傛暟
		SMTSettingPage *page = new SMTExtFuncPageUI();
		page->setOperator(this);
		return page;
	}
	else
	{
		return nullptr;
	}
}

void SMTExtFuncDemo::SetPagePt(SMTExtFuncPageUI *page)
{

}

//鍒濆鍖栬緭鍑洪摼琛?
void SMTExtFuncDemo::initOutPutVariables()
{
	//閰嶇疆杈撳嚭
	SMTVariable stepStatus(SMTVariable::VARIABLE_BOOL,
		SMTString("stepStatus").toLocal8Bit().constData(),
		QObject::tr("Boolean variable,set to TRUE if no error occurs and all outputs of current step are within the limits,otherwise FALSE.").toLocal8Bit().constData(), this);
	//the result can not be modified!
	stepStatus.setReadOnly(true);
	stepStatus.setEnabled(true);
	m_paraOutput.push_back(stepStatus);

	SMTVariable OriginalPoint(SMTVariable::VARIABLE_POINT,
		SMTString("OriginalPointOutput").toLocal8Bit().constData(),
		QObject::tr("Original Point's position.").toLocal8Bit().constData(), this);
	//the result can not be modified!
	OriginalPoint.setReadOnly(true);
	OriginalPoint.setEnabled(true);
	m_paraOutput.push_back(OriginalPoint);

	SMTVariable OffsetPoint(SMTVariable::VARIABLE_POINT,
		SMTString("OffsetPointOutput").toLocal8Bit().constData(),
		QObject::tr("After Offset ,Point's position.").toLocal8Bit().constData(), this);
	//the result can not be modified!
	OffsetPoint.setReadOnly(true);
	OffsetPoint.setEnabled(true);
	m_paraOutput.push_back(OffsetPoint);
}

//鍒濆鍖栬緭鍏ラ摼琛?
void SMTExtFuncDemo::initInputVariables()
{
	double offsetx = 0;
	double offsety = 0;
	SMTPoint inputpoint(0, 0);

	SMTVariable InputPoint(SMTVariable::VARIABLE_POINT, SMTString("InputPoint").toLocal8Bit().constData(),
		QObject::tr("InputPoint").toLocal8Bit().constData(), this);
	InputPoint.setValue(inputpoint);
	m_paraInput.push_back(InputPoint);

	//杈撳叆閾捐〃锛堢敤浜庡瓨鏀惧弬鏁帮級
	SMTVariable OffsetX(SMTVariable::VARIABLE_NUMBER, SMTString("OffsetX").toLocal8Bit().constData(),
		QObject::tr("OffsetX").toLocal8Bit().constData(), this);
	OffsetX.setValue(offsetx);
	m_paraInput.push_back(OffsetX);

	SMTVariable OffsetY(SMTVariable::VARIABLE_NUMBER, SMTString("OffsetY").toLocal8Bit().constData(),
		QObject::tr("OffsetY").toLocal8Bit().constData(), this);
	OffsetY.setValue(offsety);
	m_paraInput.push_back(OffsetY);

}

//璁剧疆灞炴€?涓€鑸笉闇€瑕佷慨鏀?
void SMTExtFuncDemo::initProperties()
{
	m_name = "OffsetPoint";
	m_ID = QUuid::createUuid().toString().toLocal8Bit().constData();
}

//璁剧疆绠楀瓙闇€瑕佺殑椤甸潰
void SMTExtFuncDemo::initSettingPages()
{
	//涓嶉渶瑕佺殑閫氱敤椤甸潰
	m_sorter.setEnabled(false);
	m_filter.setEnabled(false);

	//闇€瑕佺殑閫氱敤椤甸潰
	m_preprocessor.setEnabled(false);
	m_graphics.setEnabled(true);
	m_outputValidator.setEnabled(true);
	m_roi.setEnabled(false);
	m_roi.setEnableMask(false);

	SMTGraphicElement Original_point(SMTShape::SMT_SHAPE_POINT);
	Original_point.setName(QT_TRANSLATE_NOOP("OriginalPointGraph", "Point"));
	Original_point.setPenColor(0, 0, 255, 255);
	Original_point.setNGPenColor(255, 0, 0, 255);
	Original_point.enableDisplay(true);
	m_graphics.addElement(Original_point);

	SMTGraphicElement Offset_point(SMTShape::SMT_SHAPE_POINT);
	Offset_point.setName("OffsetPointGraph");
	Offset_point.setPenColor(0, 255, 0, 255);
	Offset_point.setNGPenColor(255, 0, 0, 255);
	Offset_point.enableDisplay(true);
	m_graphics.addElement(Original_point);

}


//鍐欏叆鏃ュ織
void SMTExtFuncDemo::simpleLog(QString fileName, QString body)
{
	QDateTime current_date_time = QDateTime::currentDateTime();
	QString current_date = current_date_time.toString("yyyy.MM.dd hh:mm:ss.zzz");

	QDir *TEST = new QDir;
	bool exist = TEST->exists("log");
	if (!exist)
		bool ok = TEST->mkdir("log");
	QString fileName1 = "log/" + fileName;

	QFile file(fileName1);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
	{
		qDebug() << "open fail!" << endl;
	}
	QTextStream in(&file);
	current_date += ":" + body;
	in << current_date << "\n";
	file.close();
	delete TEST;
}



ADAPTER_DLL_API SMTOperator* createOperator()
{
	return new SMTExtFuncDemo();
}

ADAPTER_DLL_API void releaseOperator(SMTOperator *opr)
{
	SMTExtFuncDemo *oprSMTExt = (SMTExtFuncDemo*)opr;
	delete oprSMTExt;
}

